package com.castle.fortress.admin.study.course.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.member.dto.MemberCollectDto;
import com.castle.fortress.admin.member.dto.MemberDto;
import com.castle.fortress.admin.member.service.MemberCollectService;
import com.castle.fortress.admin.member.service.MemberService;
import com.castle.fortress.admin.study.course.entity.StudyCourseEntity;
import com.castle.fortress.admin.study.course.dto.StudyCourseDto;
import com.castle.fortress.admin.study.course.service.StudyCourseChapterService;
import com.castle.fortress.admin.system.entity.SysUser;
import com.castle.fortress.admin.study.course.service.StudyCourseService;
import com.castle.fortress.common.enums.CourseStatusEnum;
import com.castle.fortress.common.enums.YesNoEnum;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.admin.utils.WebUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.admin.core.annotation.CastleLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 课程管理 api 控制器
 *
 * @author castle
 * @since 2023-01-30
 */
@Api(tags="课程管理api管理控制器")
@RestController
@RequestMapping("/api/study/course/studyCourse")
public class ApiStudyCourseController {
	@Autowired
	private StudyCourseService studyCourseService;
	@Autowired
	private StudyCourseChapterService studyCourseChapterService;
	@Autowired
	private MemberCollectService memberCollectService;
	@Autowired
	private MemberService memberService;


	/**
	 * 课程管理的分页展示
	 * @param studyCourseDto 课程管理实体类
	 * @param currentPage 当前页
	 * @param size  每页记录数
	 * @return
	 */
    @CastleLog(operLocation="课程管理",operType = OperationTypeEnum.QUERY)
	@ApiOperation("课程管理分页展示")
	@GetMapping("/page")
	public RespBody<IPage<StudyCourseDto>> pageStudyCourse(StudyCourseDto studyCourseDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
		Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
		Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
		Page<StudyCourseDto> page = new Page(pageIndex, pageSize);
		studyCourseDto.setCourseStatus(CourseStatusEnum.PUTAWAY.getCode());
		IPage<StudyCourseDto> pages = studyCourseService.pageStudyCourseExtends(page, studyCourseDto);
		return RespBody.data(pages);
	}

	/**
	 * 课程管理保存
	 * @param studyCourseDto 课程管理实体类
	 * @return
	 */
    @CastleLog(operLocation="课程管理",operType = OperationTypeEnum.INSERT)
	@ApiOperation("课程管理保存")
	@PostMapping("/save")
	public RespBody<String> saveStudyCourse(@RequestBody StudyCourseDto studyCourseDto){
		if(studyCourseDto == null ){
			throw new BizException(GlobalRespCode.PARAM_MISSED);
		}
			StudyCourseEntity studyCourseEntity = ConvertUtil.transformObj(studyCourseDto,StudyCourseEntity.class);
		if(studyCourseService.save(studyCourseEntity)){
			return RespBody.data("保存成功");
		}else{
			return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
		}
	}

	/**
	 * 课程管理编辑
	 * @param studyCourseDto 课程管理实体类
	 * @return
	 */
    @CastleLog(operLocation="课程管理",operType = OperationTypeEnum.UPDATE)
	@ApiOperation("课程管理编辑")
	@PostMapping("/edit")
	public RespBody<String> updateStudyCourse(@RequestBody StudyCourseDto studyCourseDto){
		if(studyCourseDto == null || studyCourseDto.getId() == null || studyCourseDto.getId().equals(0L)){
			throw new BizException(GlobalRespCode.PARAM_MISSED);
		}
			StudyCourseEntity studyCourseEntity = ConvertUtil.transformObj(studyCourseDto,StudyCourseEntity.class);
		if(studyCourseService.updateById(studyCourseEntity)){
			return RespBody.data("保存成功");
		}else{
			return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
		}
	}

	/**
	 * 课程管理删除
	 * @param ids 课程管理id集合
	 * @return
	 */
    @CastleLog(operLocation="课程管理",operType = OperationTypeEnum.DELETE)
	@ApiOperation("课程管理删除")
	@PostMapping("/delete")
	public RespBody<String> deleteStudyCourse(@RequestBody List<Long> ids){
		if(ids == null || ids.isEmpty()){
			throw new BizException(GlobalRespCode.PARAM_MISSED);
		}
		if(studyCourseService.removeByIds(ids)) {
			return RespBody.data("保存成功");
		}else{
			return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
		}
	}

	/**
	 * 课程管理详情
	 * @param id 课程管理id
	 * @return
	 */
    @CastleLog(operLocation="课程管理",operType = OperationTypeEnum.QUERY)
	@ApiOperation("课程管理详情")
	@GetMapping("/info")
	public RespBody<StudyCourseDto> infoStudyCourse(@RequestParam Long id){
		if(id == null){
			throw new BizException(GlobalRespCode.PARAM_MISSED);
		}
		//是否绑定课程
		boolean isBindCourse = false;
		boolean isCollect = false;
		MemberDto member = WebUtil.currentMember();
		StudyCourseDto studyCourseDto = null;
		//用户已登录
		if(member!=null){
			//是否收藏该课程
			MemberCollectDto memberCollectDto = new MemberCollectDto();
			memberCollectDto.setCollectType("00");//课程收藏
			memberCollectDto.setMemberId(member.getId());
			memberCollectDto.setTargetId(id);
			List<MemberCollectDto> list = memberCollectService.listStudyCollect(memberCollectDto);
			if(list!=null && list.size()>0){
				isCollect = true;
			}
			studyCourseDto =  studyCourseService.studyInfo(id,member.getId());
			//已绑定课程
			if(studyCourseDto != null){
				isBindCourse = true;
				studyCourseDto.setChapterList(studyCourseChapterService.getStudyListByCourseId(id,member.getId()));
			}
		}
		if(!isBindCourse){
			studyCourseDto =  studyCourseService.getByIdExtends(id);
			if(studyCourseDto != null){
				studyCourseDto.setChapterList(studyCourseChapterService.getListByCourseId(id));
			}else{
				throw new BizException(GlobalRespCode.BUSINESS_ERROR);
			}
		}
		studyCourseDto.setIsBind(isBindCourse);
		studyCourseDto.setIsCollect(isCollect);
		return RespBody.data(studyCourseDto);
	}

	/**
	 * 学习该课程的用户列表
	 * @param id 课程管理id
	 * @return
	 */
	@CastleLog(operLocation="课程管理",operType = OperationTypeEnum.QUERY)
	@ApiOperation("学习该课程的用户列表")
	@GetMapping("/userList")
	public RespBody<Map<String,Object>> studyUserList(@RequestParam Long id){
		if(id == null){
			throw new BizException(GlobalRespCode.PARAM_MISSED);
		}
		List<MemberDto> list = memberService.studyCourseUsers(id);
		int limit = 5;
		List<MemberDto> members = list;
		if(list!=null && list.size()>limit){
			members = list.subList(0,limit);
		}
		Map<String,Object> map=new HashMap<>();
		map.put("members",members);
		map.put("total",list!=null?list.size():0);
		return RespBody.data(map);
	}


	/**
	 * 课程进度列表 不包括已下架的
	 * @param studyCourseDto 课程管理实体类
	 * @return
	 */
	@CastleLog(operLocation="课程管理",operType = OperationTypeEnum.QUERY)
	@ApiOperation("课程进度列表")
	@GetMapping("/schedulelist")
	@ResponseBody
	public RespBody<List<StudyCourseDto>> scheduleList(StudyCourseDto studyCourseDto){
		MemberDto member = WebUtil.currentMember();
		if(member==null){
			throw new BizException(GlobalRespCode.NO_LOGIN_ERROR);
		}
		List<StudyCourseDto> list = studyCourseService.scheduleList(studyCourseDto,member);
		return RespBody.data(list);
	}

	/**
	 * 完成的课程列表
	 * @param studyCourseDto 课程管理实体类
	 * @return
	 */
	@CastleLog(operLocation="课程管理",operType = OperationTypeEnum.QUERY)
	@ApiOperation("课程进度列表")
	@GetMapping("/finishlist")
	@ResponseBody
	public RespBody<List<StudyCourseDto>> finishList(StudyCourseDto studyCourseDto){
		MemberDto member = WebUtil.currentMember();
		if(member==null){
			throw new BizException(GlobalRespCode.NO_LOGIN_ERROR);
		}
		List<StudyCourseDto> list = studyCourseService.finishList(studyCourseDto,member);
		return RespBody.data(list);
	}

	/**
	 * 课程收藏列表
	 * @return
	 */
	@CastleLog(operLocation="课程管理",operType = OperationTypeEnum.QUERY)
	@ApiOperation("课程收藏列表")
	@GetMapping("/collectlist")
	@ResponseBody
	public RespBody<List<StudyCourseDto>> collectList(){
		MemberDto member = WebUtil.currentMember();
		if(member==null){
			throw new BizException(GlobalRespCode.NO_LOGIN_ERROR);
		}
		List<StudyCourseDto> list = studyCourseService.collectList(member.getId());
		return RespBody.data(list);
	}



}
